package com.tjetc.controller;

import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.exceptions.ClientException;
import com.tjetc.domain.Doctor;
import com.tjetc.service.DoctorService;
import com.tjetc.service.impl.DoctorServiceImpl;
import com.tjetc.vo.Result;
import com.tjetc.util.JwtUtils;
import com.tjetc.util.JwtUtils;
import com.tjetc.vo.Result;
import com.tjetc.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * (doctor)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    /**
     * 服务对象
     */
    @Resource
    private DoctorService doctorService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public Doctor selectOne(@RequestParam("id") Integer id) {
return doctorService.selectByPrimaryKey(id);
}

@RequestMapping("getAll")
    public Result getAll(@RequestParam("hid")Integer hid,  @RequestParam("name")String name, @RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize) {
        return doctorService.getAll(hid,name, pageNum, pageSize);
    }
    // formData.append('id', form.value.id)
    //  formData.append('name', form.value.name)
    //  formData.append('sex', form.value.sex)
    //  formData.append('title', form.value.title)
    //  formData.append('desc', form.value.desc)
    //  formData.append('birthday', form.value.birthday)
    //  formData.append('entryTime', form.value.entryTime)
    //  formData.append('icon', form.value.icon)
    //  if (file.value){
    //    formData.append('file', file.value)
    //  }
    @PostMapping("save")
    public Result save(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "sex", required = false) Integer sex,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "desc", required = false) String desc,
            @RequestParam(value = "birthday", required = false) Long birthdayTimestamp, // 改为Long
            @RequestParam(value = "entryTime", required = false) Long entryTimeTimestamp, // 改为Long
            @RequestParam(value = "clinicId", required = false) Integer clinicId, // 添加门诊ID
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws IOException, ClientException {

        Doctor doctor = new Doctor();
        doctor.setId(id);
        doctor.setName(name);
        doctor.setSex(sex);
        doctor.setTitle(title);
        doctor.setDesc(desc);

        // 设置门诊ID
        doctor.setCid(clinicId);

        // 转换时间戳到Date
        if (birthdayTimestamp != null) {
            doctor.setBirthday(new Date(birthdayTimestamp));
        }

        if (entryTimeTimestamp != null) {
            doctor.setEntryTime(new Date(entryTimeTimestamp));
        }

        return doctorService.save(doctor, file);
    }
    //删除
    @RequestMapping("delete")
    public Result delete(@RequestParam("id")Integer id) {
        return doctorService.deleteById(id);
    }





    @GetMapping("selByClinicId")
    public Result selByClinicId(@RequestParam("cid") Integer cid) {
        return doctorService.selByClinicId(cid);
    }
    @PostMapping("login")
    public Result login(@RequestBody Doctor doctor) {
        return doctorService.login(doctor);
    }
    @GetMapping("/checkToken")
    public boolean checkToken(@RequestHeader("token") String token) {
        System.out.println("医生校验token");
        return JwtUtils.checkToken(token);
    }
    @GetMapping("getOne")
    public Doctor getOne(@RequestParam("id") Integer id) {
        return doctorService.getDoctorAndClinic(id);
    }
    //修改密码updatePassword
//    @RequestMapping("updatePassword")
//    public Result updatePassword(@RequestParam(value = "id",required = false)Integer id,
//                                 @RequestParam(value = "oldPassword",required = false) String oldPassword,
//                                 @RequestParam(value = "newPassword",required = false) String newPassword) {
//        System.out.println("id = " + id);
//        System.out.println("oldPassword = " + oldPassword);
//        System.out.println("newPassword = " + newPassword);
//    return doctorService.updatePassword(id,oldPassword,newPassword);
//    }
    //接收数据  axios.post('/doctor/updatePassword', {
    //      id:user.value.id,
    //      oldPassword: passwordForm.oldPassword,
    //      newPassword: passwordForm.newPassword
    @RequestMapping("updatePassword")
    public Result updatePassword(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String oldPassword = jsonObject.getString("oldPassword");
        String newPassword = jsonObject.getString("newPassword");
        return doctorService.updatePassword(id,oldPassword,newPassword);
    }
}
